package com.cybertron.ironhide.manage.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.cybertron.ironhide.manage.common.Constants;
import com.cybertron.ironhide.manage.common.base.Pager;
import com.cybertron.ironhide.manage.domain.Article;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Component
public class PageUtil {
	/**
	 * 生成静态页面主方法
	 * 
	 * @param context
	 *            ServletContext
	 * @param data
	 *            一个Map的数据结果集
	 * @param templatePath
	 *            ftl模版路径
	 * @param targetHtmlPath
	 *            生成静态页面的路径
	 * @throws Exception
	 */

	private static final String BLANK_PATTERN = "[^0-9a-zA-Z]";

	@Resource(name = "freeMarkerConfiguration")
	private Configuration configuration;

	public boolean crateHTML(Map<String, Object> data, String templatePath,
			String targetHtmlPath) {
		if (data == null) {
			data = new HashMap<String, Object>();
		}
		data.put("base", Constants.WEB_ROOT_PATH);
		data.put("resourceRootUrl", Constants.RESOURCE_ROOT_URL);
		data.put("resourceRootPath", Constants.RESOURCE_ROOT_PATH);
		data.put("search", Constants.SEARCH_CONTEXT_PATH);
		try {
			// 指定模版路径
			Template template = configuration
					.getTemplate(templatePath, "UTF-8");
			template.setEncoding("UTF-8");
			// 静态页面路径
			String htmlPath = Constants.RESOURCE_ROOT_PATH + File.separator
					+ targetHtmlPath;
			File htmlFile = new File(htmlPath);
			File htmlFileParent = htmlFile.getParentFile();
			if (!htmlFileParent.exists()) {
				htmlFileParent.mkdirs();
			}
			Writer out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(htmlFile), "UTF-8"));
			// 处理模版
			template.process(data, out);
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean createFooterHTML(List<Article> topArticles,
			List<Article> botArticles) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("topArticles", topArticles);
		data.put("botArticles", botArticles);
		crateHTML(data, "footer.ftl", "footer.html");
		return true;
	}

	public boolean createArticleHTML(List<Article> articles,
			List<Article> topArticles, List<Article> botArticles, Pager pager) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("articles", articles);
		data.put("topArticles", topArticles);
		data.put("botArticles", botArticles);
		if (pager != null) {
			data.put("pager", pager);
			crateHTML(data, "article_list.ftl", "article" + File.separator
					+ pager.getPageNum() + ".html");
		}
		for (Article article : articles) {
			data = new HashMap<String, Object>();
			data.put("article", article);
			data.put("topArticles", topArticles);
			data.put("botArticles", botArticles);
			if (article.getType() == 1) {
				crateHTML(data, "article.ftl", "article" + File.separator
						+ encodeUrl(article.getTitle()) + ".html");
			} else if (article.getType() == 2) {
				crateHTML(data, "site_map.ftl", "article" + File.separator
						+ encodeUrl(article.getTitle()) + ".html");
			}
		}
		return true;
	}

	public static String encodeUrl(String input) {
		if (StringUtils.isNotBlank(input)) {
			return input.replaceAll(BLANK_PATTERN, "-");
		} else {
			return input;
		}
	}
}
